/**
 * 设置缓存
 * @param {String} key
 * @param {*} value
 */
export const setStorage = (key, value) => {
  try {
    wx.setStorageSync(key, value)
  } catch (error) {
    console.error(`存储指定${key}的时候出现异常`, error)
  }
}

/**
 * 获取缓存
 * @param {String} key
 * @returns
 */
export const getStorage = (key) => {
  try {
    const value = wx.getStorageSync(key)
    if (value) {
      return value
    }
  } catch (error) {
    console.error(`获取指定${key}的时候出现异常`, error)
  }
}

/**
 * 删除缓存
 * @param {String} key
 */
export const removeStorage = (key) => {
  try {
    wx.removeStorageSync(key)
  } catch (error) {
    console.error(`删除指定${key}的时候出现异常`, error)
  }
}

/**
 * 清除所有缓存
 */
export const clearStorage = () => {
  try {
    wx.clearStorageSync()
  } catch (error) {
    console.error(`清除缓存的时候出现异常`, error)
  }
}

/**
 * 设置异步缓存
 * @param {String} key
 * @param {*} value
 */
export const asyncSetStorage = (key, value) => {
  return new Promise((resolve) => {
    wx.setStorage({
      key,
      data: value,
      complete(res) {
        resolve(res)
      }
    })
  })
}

/**
 * 获取异步缓存
 * @param {String} key
 * @returns
 */
export const asyncGetStorage = (key) => {
  return new Promise((resolve) => {
    wx.getStorage({
      key,
      complete(res) {
        resolve(res)
      }
    })
  })
}

/**
 * 删除异步缓存
 * @param {String} key
 */
export const asyncRemoveStorage = (key) => {
  return new Promise((resolve) => {
    wx.removeStorage({
      key,
      complete(res) {
        resolve(res)
      }
    })
  })
}

/**
 * 清除所有异步缓存
 */
export const asyncClearStorage = () => {
  return new Promise((resolve) => {
    wx.clearStorage({
      complete(res) {
        resolve(res)
      }
    })
  })
}
